package com.ymatou.iapi.payment.testcase;

import java.math.BigDecimal;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.payment.parameter.AcquireOrderBean;
import com.ymatou.iapi.payment.parameter.QueryByTradingIdBean;
import com.ymatou.iapi.payment.service.QueryByTradingIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.MPayment;
import com.ymttest.database.sqlwapper.PayGateWayWapper;

/**
 * 提交退款接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryByTradingId {
	private static QueryByTradingIdBean QueryByTradingIdBean;
	private static QueryByTradingIdCall QueryByTradingIdCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("提交退款接口");
	}

	@Before
	public void caseUp() {
		QueryByTradingIdBean = new QueryByTradingIdBean();
		QueryByTradingIdCall = new QueryByTradingIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(QueryByTradingIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_001() {
		Logger.start(true, "查询交易成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_002() {
		Logger.start(true, "查询快速退款交易成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
			PayGateWayCallService.acquireOrder(aqb);
			PayGateWayCallService.notifyCall(aqb);
			PayGateWayCallService.fastRefund(aqb);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,"0.00");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryByTradingId_003() {
		Logger.start(true, "查询第三方优惠记录成功");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("50");
			aqb.setPayPrice("6.00");
			PayGateWayCallService.acquireOrder(aqb);
			String discountAmt = "1.00";
			PayGateWayCallService.notifyCall50(aqb, discountAmt);
			QueryByTradingIdBean.setTradingId(aqb.getOrderId());
			QueryByTradingIdCall.setData(QueryByTradingIdBean);
			QueryByTradingIdCall.callService();
			Logger.comment("验证返回信息");
			Map m = QueryByTradingIdCall.getMap();
			Logger.verifyEquals(0d, m.get("ErrorCode"), "验证ErrorCode");
			verifyDB(aqb,discountAmt);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyDB(AcquireOrderBean aqb, String discountAmt) throws Exception {
		Logger.comment("验证数据库");
		PayGateWayWapper paymentwapper = new PayGateWayWapper();
		MPayment ppm = paymentwapper.selectPaymentByTradingid(aqb.getOrderId()).get(0);
		JsonObject result = QueryByTradingIdCall.getJsonObject().get("Result").getAsJsonObject();
		Logger.verifyEquals(result.get("PaymentId").getAsString(), ppm.getPaymentId(), "验证PaymentId");
		Logger.verifyEquals(result.get("PayType").getAsString(), ppm.getPayType().toString(), "验证PayType");
		Logger.verifyEquals(result.get("PayStatus").getAsInt(), ppm.getPayStatus(), "验证PayStatus");
		Logger.verifyEquals(result.get("InstitutionPaymentId").getAsString(), ppm.getInstitutionPaymentId(), "验证InstitutionPaymentId");
		Logger.verifyEquals(new BigDecimal(result.get("OrderAmt").toString()).setScale(2), ppm.getOrderAmt().setScale(2), "验证PayPrice");
		Logger.verifyEquals(new BigDecimal(result.get("DiscountAmt").toString()).setScale(2),ppm.getDiscountAmt().setScale(2), "验证DiscountAmt");
		Logger.verifyEquals(new BigDecimal(discountAmt).setScale(2), ppm.getDiscountAmt().setScale(2), "验证入参DiscountAmt");
	}
}
